package com.qeegoo.b2bautozityp.moudle.good.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.moudle.good.view.carmodel.CarModelOneFragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 王少岩 在 2017/9/22 创建了它
 */

public class CarModelGridAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {

    private JSONArray list;
    private Context mContext;
    private Fragment mFragment;

    public CarModelGridAdapter(Fragment fragment, JSONArray list) {
        this.mContext = fragment.getActivity();
        this.mFragment = fragment;
        this.list = list;
    }

    @Override
    public int getCount() {
        int len = list.length();
        int a = len % 4;
        if (len < 4) {
            return 4;
        }
        if (a == 0) {
            return len;
        } else {
            return len - a + 4;
        }
    }

    @Override
    public Object getItem(int position) {
        try {
            return list.get(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_goods_carmodel_grid_item, parent, false);
            viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.image_search_logo);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_search_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (position < list.length()) {
            try {
                JSONObject jsonObject = (JSONObject) getItem(position);
                viewHolder.tv_name.setText(jsonObject.has("logoName") ? jsonObject.getString("logoName") : "");
                Glide.with(mContext).load(jsonObject.has("logoUrl") ? jsonObject.getString("logoUrl") : "").placeholder(R.mipmap.placeholder).error(R.mipmap.placeholder).into(viewHolder.iv_image);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return convertView;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try {
            JSONObject jsonObject = (JSONObject) getItem(position);
            String logoName = jsonObject.getString("logoName");
            String carId = jsonObject.getString("id");
            mFragment.getParentFragment().getFragmentManager().beginTransaction()
                    .add(R.id.layout_container, CarModelOneFragment.newInstance(logoName, carId))
                    .addToBackStack(null)
                    .commitAllowingStateLoss();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    class ViewHolder {
        ImageView iv_image;
        TextView tv_name;
    }
}
